home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5994 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: newsjunkie.ans.net!philabs!usenet
  2. From: abf@philabs.research.philips.com (Andrew Feldman)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Problems extending an existing class library
  5. Date: Wed, 07 Feb 1996 22:07:20 GMT
  6. Organization: Philips Laboratories, Briarcliff, NY 10510
  7. Distribution: inet
  8. Message-ID: <4fat52$cnk@philabs.research.philips.com>
  9. References: <4fanik$ltq@scapa.cs.ualberta.ca>
  10. NNTP-Posting-Host: idrpc5.philabs.research.philips.com
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. diego@cs.ualberta.ca (Diego Novillo) wrote:
  14.  
  15. >We are developing a compiler using a code restructuring tool that
  16. >provides classes for all the basic components of a program: Functions,
  17. >Statements, Expressions, Types, Symbols and their corresponding
  18. >sub-classes.
  19.  
  20. >Now, we would like to create new sub-classes to specialize the existing
  21. >classes and the first impulse was to inherit from them because the
  22. >specialization was clearly an "is-a" relation.
  23.  
  24. >This system parses the source code and builds a special file which
  25. >contains the annotated parse tree of the program. All the objects reside
  26. >on this binary file and the memory objects are just pointers to
  27. >different portions of the file. As a result, the memory objects are just
  28. >pointers to C data structures which hold the real implementation of the
  29. >system (This class library is a wrapper around the old C
  30. >implementation).
  31.  
  32. >Since the base classes don't have constructors, we cannot build our own
  33. >memory objects because our constructors cannot call the base class
  34. >constructors to initialize the parent class.
  35.  
  36. >We got around the problem by using a "has-a" relation. That is, each of
  37. >our classes has a pointer to the "parent" class and each time we create
  38. >a new object we just initialize this pointer.
  39.  
  40. >We don't like this option too much, but we don't see another way around
  41. >the problem. Modifying the original classes is not an option either, the
  42. >system is large and we don't have much time to mess around with it.
  43.  
  44. >Does anyone have a suggestion on how to work around this problem? Has
  45. >anyone faced this problem before? How did you solve it?
  46.  
  47. >Thank you.
  48.  
  49. >Diego.
  50.  
  51.  
  52. What I think is good in this case is using a "is-a" relation as it is.
  53. You CAN inherit a class from C struct; C struct doesn't have a
  54. constructor - so what? It must definitely have some function which
  55. initializes it; just call this function in the child's constructor. If
  56. you want to inherit many C++ classes from one C struct, maybe it would
  57. be useful to create a "wrapper" for this struct  - derived class with
  58. just constructor(s) and a destructor.
  59.  
  60. Hope this advise will be useful.
  61. Andrew.
  62.  
  63.  
  64.